package com.ZeroCarbon.service.impl;

import com.ZeroCarbon.domain.dto.UsernameAndAvatarDTO;
import com.ZeroCarbon.domain.po.UserComment;
import com.ZeroCarbon.exception.BusinessLogicException;
import com.ZeroCarbon.mapper.UserCommentMapper;
import com.ZeroCarbon.service.IUserCommentService;
import com.ZeroCarbon.utils.UserHolder;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.ZeroCarbon.BusinessUtils.UserClientUtils.*;
import static com.ZeroCarbon.exception.BusinessLogicException.ERROR_EXCEPTION;
import static com.ZeroCarbon.method.RemoteClientCall.OperationType.GET_AVATAR;
import static com.ZeroCarbon.method.RemoteClientCall.OperationType.GET_USERNAME;

@Service
@RequiredArgsConstructor
public class UserCommentServiceImpl extends ServiceImpl<UserCommentMapper, UserComment> implements IUserCommentService {

    /**
     * 添加用户与评论关系
     *
     * @param id 评论id
     */
    @Override
    public Boolean postComment(String id) throws BusinessLogicException {
        //获取用户id
        String userId = UserHolder.getUser();
        //获取用户评论对象
        UserComment userComment = new UserComment(id, userId);
        try {
            //保存信息
            return save(userComment);
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 获取评论对应用户名和头像
     *
     * @param id 评论id
     */
    @Override
    public UsernameAndAvatarDTO getUsernameAndAvatar(String id) {
        //获取UserComment对象
        UserComment userComment = getById(id);
        //获取用户id
        String userId = userComment.getUserId();
        //获取头像和姓名
        Map<String, String> userClientInformation = getUserClientInformation(userId, GET_USERNAME, GET_AVATAR);
        //返回结果
        return new UsernameAndAvatarDTO(userId,
                userClientInformation.get(USERNAME),
                userClientInformation.get(AVATAR));
    }
}
